//
//  UIBarButtonItem+Extension.swift
//  WeiBo
//
//  Created by TanJian on 15/12/5.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit

let ItemTitleColor: UIColor = UIColor(white: 80/255, alpha: 1)
let ItemTitleFontSize: CGFloat = 14

extension UIBarButtonItem {
    
    //重写UIBarButtonItem的构造方法
    convenience init(title: String? = nil, imgName: String? = nil, target: AnyObject?, action: Selector){
        
        self.init()
        
        let button = UIButton()
        
        button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        
        //有图片则设置图片
        if let image = imgName {
            
            button.setImage(UIImage(named: image), forState: .Normal)
            button.setImage(UIImage(named: "\(image)_highlighted"), forState: .Highlighted)
            
        }
        //有文字则设置文字
        if let text = title {
            
            button.setTitle(text, forState: .Normal)
            button.setTitleColor(ItemTitleColor, forState: .Normal)
            button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            button.titleLabel?.font = UIFont.systemFontOfSize(ItemTitleFontSize)
            
        }
        
        //如果不设置frame，必须sizetofit，否则看不见视图，但有响应
        button.sizeToFit()
        customView = button
    }
    
    
}